package com.example.blog.config;

import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.util.Date;

/**
 * @ClassName DateFormatSerializer
 * @Description TODO
 * @Author Wang Li Hong
 * @date 2024/1/12 18:02
 * @Version 1.0
 */
public class DateFormatSerializer extends JsonSerializer<Date> {
    @Override
    public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (date != null) {
            String format = DateUtil.format(date, "yyyy-MM-dd HH:mm:ss");
            jsonGenerator.writeString(format);
        }
    }
}
